<?php

namespace App\Http\Controllers\Api;

use App\Http\Models\Activity;
use App\Http\Models\Medical;
use App\Models\ActivityAreaSetting;
use App\Models\ActivityInformation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

/**
 * 活动专题
 *
 * @Author hechengwei
 * @email chichiooo@sina.com
 * @DateTime 2022-12-20
 */
class ActivityController extends BaseController
{
    /**
     * 病例活动专区列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-20
     * @param Request $request
     * @return json
     */
    public function ActivityTopicList(Request $request)
    {
        $rules = [

            'page' => ['required'],
            'limit' => ['required'],
        ];

        $message = [
            'page.required' => '页码不能为空',
            'limit.required' => '限制条数不能为空',
        ];

        $payload = $request->only('page', 'limit');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        // $where['name'] = $request->input('name');
        $where['cate_id'] = $request->input('cate_id');

        $model = new Activity();
        $list = $model->getList((int) $payload['page'], (int) $payload['limit'], $where, ['id', 'activity_name', 'cover', 'desc', 'created_at']);

        return success($list);
    }


    /**
     * 进入活动专题病例列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-12-20
     * @param Request $request
     * @return void
     */
    public function medicalTopicList(Request $request)
    {
        return success();
        $rules = [
            'act_id' => ['required'],
            'page' => ['required'],
            'limit' => ['required'],
        ];

        $message = [
            'act_id.required' => '专题Id不能为空',
            'page.required' => '页码不能为空',
            'limit.required' => '限制条数不能为空',
        ];

        $payload = $request->only('act_id', 'page', 'limit', 'cate_id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $where['act_id'] = $payload['act_id'];
        $where['cate_id'] = $payload['cate_id'];

        $model = new Medical();
        $list = $model->getMedicalByActIdList((int) $payload['page'], (int) $payload['limit'], $where, ['id', 'title', 'medical_history', 'author', 'created_at']);
        $list['detail'] = Activity::where('id', $payload['act_id'])->select('id', 'activity_name', 'desc', 'cover')->first();
        return success($list);
    }


    /**
     * 进入活动专题病例列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-07
     * @param Request $request
     * @return void
     */
    public function medicalTopicListNew(Request $request)
    {
        // return success();
        $rules = [
            'act_id' => ['required'],
            'page' => ['required'],
            'limit' => ['required'],

        ];

        $message = [
            'act_id.required' => '专题Id不能为空',
            'page.required' => '页码不能为空',
            'limit.required' => '限制条数不能为空',
        ];

        $payload = $request->only('act_id', 'page', 'limit', 'cate_id', 'area_id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $where['act_id'] = $payload['act_id'];
        $where['cate_id'] = isset($payload['cate_id']) ? $payload['cate_id'] : '';
        $where['area_id'] = isset($payload['area_id']) ? $payload['area_id'] : '';

        $model = new Medical();
        $list = $model->getMedicalByActIdListNew((int) $payload['page'], (int) $payload['limit'], $where, ['id','area_id as area', 'title', 'medical_history', 'author', 'created_at']);
        $list['detail'] = Activity::where('id', $payload['act_id'])->select('id', 'activity_name', 'desc', 'cover')->first();
        return success($list);
    }


    /**
     * 专题分类列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-01-05
     * @return void
     */
    public function topicCategoryList()
    {
        return success(Activity::getTopicCategoryList());
    }


    /**
     * 获取赛区分类
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-06
     * @param Request $request
     * @return void
     */
    public function getDivisionArea(Request $request)
    {
        return success();
        $rules = [
            'act_id' => ['required'],
        ];

        $message = [
            'act_id.required' => '专题Id不能为空',
        ];

        $payload = $request->only('act_id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        return success(ActivityAreaSetting::getDivisionArea($payload['act_id']));
    }

    /**
     * 获取赛区信息
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-06
     * @param Request $request
     * @return void
     */
    public function divisionInformation(Request $request)
    {
        $rules = [
            'act_id' => ['required'],
        ];

        $message = [
            'act_id.required' => '专题Id不能为空',
        ];

        $payload = $request->only('act_id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        return success(Activity::getDivisionInfo($payload['act_id']));
    }


    /**
     * 下载病例模板
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-09
     * @param Request $request
     * @return void
     */
    public function downloadMedicalTemplate(Request $request)
    {
        $rules = [
            'act_id' => ['required'],
        ];

        $message = [
            'act_id.required' => '专题Id不能为空',
        ];

        $payload = $request->only('act_id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        return success(ActivityInformation::geMedicalTemplate($payload['act_id']));
    }
}
